/*This is about the parser of the XML result of Yahoo Boss API.
 * After sending doGet request to Yahoo API, the user would receive
 * the response from Yahoo. We need to parser the return result.
 * Show the result to user. According to our need, we need to retrieve 
 * the total number of matches and for the purpose of second part, the
 * first four pages' url.
 * 
 * */

import java.io.BufferedReader;



import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.util.*;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.*;
public class XML {	
	
	public static List<Integer> reqResp(String query, String api) throws Exception{
		
		List<Integer> relevance= new ArrayList<Integer>();
		
		String request="http://boss.yahooapis.com/ysearch/web/v1/"+query+"?appid="+api+"&format=xml";

		URL url = new URL(request);
		
        
       InputStream rstream= url.openStream();
		
        Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(rstream);
       
        //Get all search Result nodes
        
		NodeList nodeList = doc.getElementsByTagName("result");
		// System.out.println(nodeList.getLength());
		
		if(nodeList.getLength()!=10){
			
			System.out.println("Oops, we can not find enough query you request.");
			System.exit(0);
		}
		
		System.out.println("Total number of resutl: "+nodeList.getLength());
		System.out.println("Yahoo! Boss return result:");
		System.out.println("============================");
		for (int index = 0; index < nodeList.getLength(); index++) {
			
			// to retrieve the url, summary, title result one by one
			
			String Title = null;
			String URL = null;
			String Abstract = null;

			Node node = nodeList.item(index);
			if (node.getNodeType() == Node.ELEMENT_NODE) {
				Element element = (Element) node;

				NodeList nameNode = element.getElementsByTagName("title");

				if (nameNode.item(0).getNodeType() == Node.ELEMENT_NODE) {
					Element nameElement = (Element) nameNode.item(0);
				     Title = nameElement.getFirstChild().getNodeValue()
							.trim();
					// System.out.println("TITLE: " + theTitle);
				}

				nameNode = element.getElementsByTagName("url");

				if (nameNode.item(0).getNodeType() == Node.ELEMENT_NODE) {
					Element nameElement = (Element) nameNode.item(0);
					URL = nameElement.getFirstChild().getNodeValue().trim();
			
				}

				nameNode = element.getElementsByTagName("abstract");

				if (nameNode.item(0).getNodeType() == Node.ELEMENT_NODE) {
					Element nameElement = (Element) nameNode.item(0);
					if (nameElement.getFirstChild() == null) {
						Abstract = "";
					} else {
						Abstract = nameElement.getFirstChild().getNodeValue();
					}
				
				}
				
				// in the search result the query word would be bold, eliminate them.
				
				Title=Title.replace("<b>","");			
				Title=Title.replace("</b>", "");
				Abstract=Abstract.replace("<b>", "");
				Abstract=Abstract.replace("</b>", "");
				
				
				int no = index + 1;
				System.out.println("Result" + no);
				 System.out.println("Title: " + Title);
		            System.out.println("Summary: " + Abstract);
		            System.out.println("URL: " + URL);
		            System.out.println("--");
		            
		            System.out.println("relevance?(Y/N)");
		            
		            //get the feedback from the user.
		            
		            BufferedReader stdin= new BufferedReader( new InputStreamReader(System.in));
		            String result=stdin.readLine(); 
		            
		            if(result.contentEquals("Y") || result.contentEquals("y")){
		            	
		            	relevance.add(index);
		            }
		            
		            // write all the result in the file for tf_idf processing.
		            
		            PrintWriter sumOut=new PrintWriter(
		            				new BufferedWriter(new FileWriter("input/"+index+".txt")));
		       
		            
		            String t=Title+ " " + Title+ " ";
		            
		            sumOut.println(t+" "+Abstract+" "+URL);
		            sumOut.close();
			}
		}
		
		//all the relevance document index would be return for further proccessing
		return relevance;
		
	}
	
}
